package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class JDBCDemo1 {
    public static void main(String[] args) throws Exception {
        //1.加载驱动
        //不同数据库提供的驱动类名字不一样
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection= DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                "root",
                "root"
        );
        System.out.println("成功连接数据库!");
        ///3
        Statement statement=connection.createStatement();
        //4
        /*
          CREATE TABLE userinfo(
              id INT PRIMARY KEY AUTO_INCREMENT,
              username VARCHAR(32),
              password VARCHAR(32),
              nickname VARCHAR(32),
              age INT(3)
          )

         */
        String sql= " CREATE TABLE userinfo(" +
                    "id INT PRIMARY KEY AUTO_INCREMENT," +
                    "username VARCHAR(32)," +
                    "password VARCHAR(32)," +
                    "nickname VARCHAR(32)," +
                    "age INT(3)" +
                    ")";

        /*
          5.该函数是Java中的一个数据库操作函数，用于执行SQL语句
          statement提供了用于执行SQL语句的方法
          boolean execute(String sql)
          执行给定的SQL语句,如果执行后存在查询集结果则返回true

        */
        statement.execute(sql);
        System.out.println("创建表成功!");
        //6
        connection.close();
    }

}
